package DAO.Impl;

import DAO.PasswordDAO;
import domain.UserPwd;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.ArrayList;
import java.util.List;

/**
 * @description: 数据库连接操作层实现类
 * @author: hank
 * @create: 2021-03-28 21:25
 **/
@SuppressWarnings("JavaDoc")
public class PasswordDAOImpl implements PasswordDAO {
    private final Connection conn;
    private PreparedStatement pstmt;

    public PasswordDAOImpl(Connection conn) {
        this.conn = conn;
    }

    @Override
    public List<UserPwd> getPassword(String user) throws SQLException {
        String sql="SELECT password FROM user_pwd WHERE user_id = ?";
        this.pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, user);
        ResultSet rs = this.pstmt.executeQuery();
        List<UserPwd> all = new ArrayList<UserPwd>();
        while (rs.next()) {
            UserPwd vo = new UserPwd();
            vo.setPassword(rs.getString(1));
            all.add(vo);
        }
        return all;
    }

    @Override
    public int saveUserPwd(String user,String password) throws SQLException {
        String sql = "INSERT into user_pwd VALUES (?,?)";
        this.pstmt = conn.prepareStatement(sql);
        pstmt.setString(1, user);
        pstmt.setString(2, password);
        int OK = this.pstmt.executeUpdate();
        return OK;
    }
}
